package cn.iocoder.yudao.module.contract.controller.admin.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.client.ClientDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBankDO;
import cn.iocoder.yudao.module.contract.service.basicData.BasicBankService;

@Tag(name = "管理后台 - 银行信息")
@RestController
@RequestMapping("/contract/basic-bank")
@Validated
public class BasicBankController {

    @Resource
    private BasicBankService basicBankService;

    @PostMapping("/create")
    @Operation(summary = "创建银行信息")
    @PreAuthorize("@ss.hasPermission('contract:basic-bank:create')")
    public CommonResult<Long> createBasicBank(@Valid @RequestBody BasicBankSaveReqVO createReqVO) {
        return success(basicBankService.createBasicBank(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新银行信息")
    @PreAuthorize("@ss.hasPermission('contract:basic-bank:update')")
    public CommonResult<Boolean> updateBasicBank(@Valid @RequestBody BasicBankSaveReqVO updateReqVO) {
        basicBankService.updateBasicBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除银行信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:basic-bank:delete')")
    public CommonResult<Boolean> deleteBasicBank(@RequestParam("id") Long id) {
        basicBankService.deleteBasicBank(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得银行信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:basic-bank:query')")
    public CommonResult<BasicBankRespVO> getBasicBank(@RequestParam("id") Long id) {
        BasicBankDO basicBank = basicBankService.getBasicBank(id);
        return success(BeanUtils.toBean(basicBank, BasicBankRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得银行信息分页")
    @PreAuthorize("@ss.hasPermission('contract:basic-bank:query')")
    public CommonResult<PageResult<BasicBankRespVO>> getBasicBankPage(@Valid BasicBankPageReqVO pageReqVO) {
        PageResult<BasicBankDO> pageResult = basicBankService.getBasicBankPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicBankRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出银行信息 Excel")
    @PreAuthorize("@ss.hasPermission('contract:basic-bank:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasicBankExcel(@Valid BasicBankPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicBankDO> list = basicBankService.getBasicBankPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "银行信息.xls", "数据", BasicBankRespVO.class,
                        BeanUtils.toBean(list, BasicBankRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得银行信息列表")
    public CommonResult<List<BasicBankDO>> getBasicBankList(@Valid BasicBankDO basicBankDO) {
        List<BasicBankDO> listResult = basicBankService.list(Wrappers.lambdaQuery(basicBankDO));
        return success(listResult);
    }

}